package io.zt.dangyuan;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/7/6.
 */
public class Utils {
    public static HashMap<String, String> map2toJson(Map<String, Object> requestMap) {
        HashMap<String, String> map = new HashMap<String, String>();
        StringBuffer sb = new StringBuffer();

        sb.append("{");
        for (String key : requestMap.keySet()) {
            sb.append("\"" + key + "\":\"" + requestMap.get(key) + "\",");
        }
        sb.replace(sb.length() - 1, sb.length(), "");
        sb.append("}");
        map.put("Json", sb.toString());
        return map;
    }
    public static String str2md5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }
}
